import Router from 'koa-router'
import httpErrors from '../../../../core/HttpException'
import Mysql from '../../../../core/Mysql'
import Auth from '../../../../middlewares/Auth'
import { getTreeByList } from '../../../../common/utils/utils'

const router = new Router({
  prefix: '/api/v1/lowCode/componentCategoryManager',
})

/*
 * 获取分类树形结构
 * @return
 */
router.get('/getComponentCategoryTree', Auth.verifyToken, async () => {
  const rootId = 0
  const res = await Mysql.command(`
    SELECT
      *
    FROM
      component_category
  `)

  // const list = res.results[0][name].split(',')
  throw new httpErrors.Success(getTreeByList(res.results, rootId))
})

export default router

interface Child {
  parentId: number | string
  children: Child[]
  [props: string]: any
}

// function getChild(id: number | string, orginization: Child[], newArray: Child[] = []) {
//   for (let i = 0; i < orginization.length; i++) {
//     undefined
//     if (orginization[i].parentid === id) {
//       undefined
//       newArray.push(orginization[i])
//       getChild(orginization[i].id, orginization)
//     } else {
//       continue
//     }
//   }
// }
